package com.base.commons.tools.utils;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {
	private static final String TRADE_FORMAT = "yyyyMMdd";
	
	private static final String TIME_FORMAT = "yyyyMMddHHmmssSSS";
	
	private static final String CONTRACT_FORMAT = "yyyy 年   MM 月 dd 日";
	
	private static final String DAY_TIME_FORMAT = "yyyy-MM-dd";
	
	private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	
	private static final String MONTHANDDAY_FORMAT ="MM 月 dd 日";
	
	private static String YEAR_MONTH_FORMAT = "yyyyMM";
	
	/**
	 * 根据“yyyy-MM-dd”字符串获取日期
	 * @return
	 */
	public static Date getDateByDayFormat(String dateString) {
		try {
			return new SimpleDateFormat(DAY_TIME_FORMAT).parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 根据“yyyy-MM-dd HH:mm:ss”字符串获取日期
	 * @return
	 */
	public static Date getDateTimeByDayFormat(String dateString) {
		try {
			return new SimpleDateFormat(DATE_TIME_FORMAT).parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获取日期 格式 “yyyy 年   MM 月 dd 日”
	 * @return
	 */
	public static String getFormatDateByDate(Date date) {
		return new SimpleDateFormat(CONTRACT_FORMAT).format(date);
	}
	
	/**
	 * 获取日期 格式 “yyyy-MM-dd HH:mm:ss”
	 * @return
	 */
	public static String getFormatDateTimeByDate(Date date) {
		return new SimpleDateFormat(DATE_TIME_FORMAT).format(date);
	}
	
	/**
	 * 获取日期 格式 "MM 月 dd 日”
	 * @return
	 */
	public static String getFormatMonthAndDayByDate(Date date) {
		return new SimpleDateFormat(MONTHANDDAY_FORMAT).format(date);
	}
	
	/**
	 * 获取当前日期 格式 “20180419”
	 * @return
	 */
	public static String getCurrentDate() {
		return new SimpleDateFormat(TRADE_FORMAT).format(new Date());
	}
	
	/**
	 * 获取当前日期 格式 “20180419”
	 * @return
	 */
	public static String getFormatYearMonthDayByDate(Date date) {
		return new SimpleDateFormat(TRADE_FORMAT).format(date);
	}

	/**
	 * 获取当前日期 格式 “2018-04-19”
	 * @return
	 */
	public static String getFormatYearMonthDayByDateBar(Date date) {
		return new SimpleDateFormat(DAY_TIME_FORMAT).format(date);
	}
	
	/**
	 * 获取时间戳，格式“20180420113136502”
	 * @return
	 */
	public static String getTimeStamp() {
		return new SimpleDateFormat(TIME_FORMAT).format(new Date());
	}
	
	/**
	 * 功能描述：时间相减得到天数
	 * 
	 * @param beginDate
	 * @param endDate
	 * @return
	 */
	public static int getDaySub(Date beginDate, Date endDate) {
		int day = 0;
		day = (int) ((endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000));
		return day;
	}
	
	public static Date getZeroDay() {
		Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date zero = calendar.getTime();
        return zero;
	}
	
	/**
	 * 获取时间段之后的时间
	 * @param date
	 * @param day
	 * @return
	 */
	public static Date getDateByday(Date date,int day) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, day);
		return calendar.getTime();
	}
	
	/**
	 * 获取时间段之后的时间
	 * @param date
	 * @param date,month
	 * @return
	 */
	public static Date getDateByMonth(Date date,int month) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, month);
		return calendar.getTime();
	}

	/**
	 * 获取今天还剩下多少秒
	 * @return
	 */
	public static int getMiao(){
		Calendar curDate = Calendar.getInstance();
		Calendar tommorowDate = new GregorianCalendar(curDate
				.get(Calendar.YEAR), curDate.get(Calendar.MONTH), curDate
				.get(Calendar.DATE) + 1, 0, 0, 0);
		return (int)(tommorowDate.getTimeInMillis() - curDate .getTimeInMillis()) / 1000;
	}
	
	/**
	 * 获取最近days的毫秒时间戳
	 * @return
	 */
	public static String getLastDaysDateStr(int days) {
		String result = "";
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.DATE, -days);
			result = calendar.getTimeInMillis() + "";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 *
	 * @param date
	 * @return
	 */
	public static Date getPreDayDate(Date date,int days){
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.DATE, days);
			return calendar.getTime();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}
	
	/**
	 * 毫秒时间戳转时间
	 * @param time
	 * @return
	 */
	public static String timeStamp2Date(String time) {
		if(StringUtils.isBlank(time)) {
			return null;
		}
	    Long timeLong = Long.parseLong(time);
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//要转换的时间格式
	    Date date;
	    try {
	        date = sdf.parse(sdf.format(timeLong));
	        return sdf.format(date);
	    } catch (ParseException e) {
	        e.printStackTrace();
	        return null;
	    }
	}
	
	/**
	 * 毫秒时间戳转时间
	 * @param time
	 * @return
	 */
	public static String timeStamp2Date(Long time) {
		if(time == null) {
			return null;
		}
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");//要转换的时间格式
	    Date date;
	    try {
	        date = sdf.parse(sdf.format(time));
	        return sdf.format(date);
	    } catch (ParseException e) {
	        e.printStackTrace();
	        return null;
	    }
	}
	
	/**
	 * 月份区间，例如当前月份201709，region=6，则月份区间为201703——201709
	 * @param region
	 * @return
	 */
	public static Map<String, String> getYYYYMMRegion(int region) {
		Map<String, String> map = new HashMap<String, String>();
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(YEAR_MONTH_FORMAT);
		String nowYM = sdf.format(date);
		map.put("endYM", nowYM);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, -region);
		String startYM = sdf.format(calendar.getTime());
		map.put("startYM", startYM);
		return map;
	}
	
	/**
	* CTS转date
	* @param str
	* @param pattern
	* @param locale
	* @return
	*/
	public static Date parse(String str, String pattern, Locale locale) {
		if (str == null || pattern == null) {
			return null;
		}
		try {
			return new SimpleDateFormat(pattern, locale).parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		int ii = getMiao();
		System.out.println(ii);
	}
	
}
